package com.gitee.feizns.quickstart.mybatis.plus.query;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.feizns.quickstart.domain.page.PageRecordsVo;
import com.gitee.feizns.quickstart.domain.page.PageVo;

import java.util.List;

/**
 * mybatis plus的分页{@link PageVo}实现类
 * @author feizns
 * @since 2023/2/14
 */
public class MybatisPlusPageRecordsVoImpl<T> implements PageRecordsVo<T> {

    /**
     * {@link IPage}
     */
    private final IPage<T> page;

    /**
     * （实现）
     * @param page 分页
     */
    public MybatisPlusPageRecordsVoImpl(IPage<T> page) {
        this.page = page;
    }

    @Override
    public PageVo getPage() {
        return new PageVo() {

            @Override
            public Long getSize() {
                long size = page.getSize();
                return PageVo.LARGEST_PAGE_SIZE != size ? size : getTotal();
            }

            @Override
            public Long getPages() {
                return page.getPages();
            }

            @Override
            public Long getCurrent() {
                return page.getCurrent();
            }

            @Override
            public Long getTotal() {
                return page.getTotal();
            }

        };
    }

    @Override
    public List<T> getRecords() {
        return page.getRecords();
    }

}
